﻿$(function(){
	formInit('#userAccounForm li.check');
	passwordCheck('#currentPassword','#passWord','#passWord1');
	$('#userAccounForm').submit(function(){editSubmit(this);return false ; });
});

function formInit(obj){
	$(obj).append('<span class="info"></span>');
}

function editSubmit(obj){
	var $obj = $(obj);
	var $info = $obj.find('.globalInfo').addClass('highMsg');
	if(editCheck(obj)){
		$obj.find('.submit').attr('disabled',true);
		$.openWin({'msg':loading,'canClose':false });
		var url = $obj.attr('action')+'&time='+time();
		var submitData ={'currentPassword':$('#currentPassword').val(),'passWord':$('#passWord').val(),'passWord1':$('#passWord1').val(),'dataType':'ajax'};
		$.post(url,submitData,function(data){
			if('ok'==data.status){
				$.openWin({'msg': data.msg,'time':2000});
				$(obj).find('input.text').val('');
			}else{ }
			$info.html(data.msg);
			$.openWin({'time':1});
			$obj.find('.submit').attr('disabled',false);
		},'json');
	}else{
		$.openWin({msg:'请正确填写<span class="high">所有</span>.','time':2000});
	}
}

function passwordCheck(passCurrent,pass,pass1){
	var $obj0 = $(passCurrent);
	var $obj = $(pass);
	var $obj1 = $(pass1);
	var $alt0 = $obj0.parent().find('span.info');
	var $alt = $obj.parent().find('span.info');
	var $alt1 = $obj1.parent().find('span.info');
	
	$obj0.blur(function(){
		if( $(this).val().length < 6 ){
			$alt0.html('<i status="error" class="error">密码不正确.</i>');
		}else
			$alt0.html('<i status="ok"></i>');
	}).focus(function(){
		$alt0.html('');
	});
	
	$obj.blur(function(){
		if( $(this).val().length < 6 ){
			$alt.html('<i status="error" class="error">密码长度必须大于 6 位</i>');
		}else
			$alt.html('<i status="ok"></i>');
	}).focus(function(){
		$alt.html('');
	});
	$obj1.blur(function(){
		if( $(this).val() != $obj.val() )
			$alt1.html('<i status="error" class="error">两次密码必须相同.</i>');
		else if( $(this).val() != '' )
			$alt1.html('<i status="ok"></i>');
	}).focus(function(){
		$alt1.html('');
	});
};

function editCheck(obj){
	var $obj = $(obj);
	var flag = true;
	$obj.find('li.check').each(function(){
		if( $(this).find('i').attr('status') != 'ok' ) flag = false;
	});
	return flag;
};